package com.ruoyi.business.component.mqconsumer.repaymentconsumers;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.bizsys.domain.ExtTransferResult;
import com.ruoyi.bizsys.domain.RepaymentOrder;
import com.ruoyi.bizsys.mapper.RepaymentOrderMapper;
import com.ruoyi.bizsys.service.IExtTransferResultService;
import com.ruoyi.bizsys.utils.BizConstants;
import com.ruoyi.business.service.IPaystackRepaymentService;
import com.ruoyi.common.utils.Func;
import com.ruoyi.extdata.common.Constants;
import com.ruoyi.extdata.service.IExternalApiService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * PaystackVaWebhookConsumer
 * @author wqg
 * @description
 * @date 2022/8/10
 */
@Slf4j
@Service(BizConstants.ROCKMQ_REPAYMENT_TYPE_WEBHOOK_VA)
public class PaystackVaWebhookConsumer implements IRepaymentMqService{
    @Autowired
    IExtTransferResultService transferResultService;

    @Autowired
    IPaystackRepaymentService iPaystackRepaymentService;
    /**
     * @param msgObj
     */
    @Override
    public void handle(JSONObject msgObj) {
        log.info("Webhook VA Repaymentstart：data:{}", msgObj.toString());
        String reference = msgObj.getString("reference");
        String custNo = msgObj.getString("custNo");
        ExtTransferResult transferResult = transferResultService.getOne(
                new QueryWrapper<ExtTransferResult>()
                        .eq("serial_no",reference)
                        .eq("state", Constants.BIZ_STATUS_1));
        if (Func.isNotEmpty(transferResult) && 1 == transferResult.getStatus()) {
            log.info("transferResult已经被消费:"+reference);
            return;
        }
        transferResult.setCustNo(custNo);
        // 开始消费
        iPaystackRepaymentService.repaymentWebHookForVa(reference,transferResult);

        log.info("Webhook VA Repaymentend：data:{}",msgObj);

    }
}
